﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace Thaire.Investment.Repository
{
    public class BaseRepository<E,PK>
    {
        protected ISession _session;
        public BaseRepository(ISession session)
        {
            _session = session;
        }

        public void Save(E entity)
        {
            _session.Save(entity);
        }

        public void Update(E entity)
        {
            _session.Update(entity);
        }

        public void Delete(E entity)
        {
            _session.Delete(entity);
        }

        public void DeleteByID(PK pk)
        {

            Delete(GetByID(pk));
        }

        public E GetByID(PK primaryKey)
        {
           return _session.Get<E>(primaryKey);
        }

        public IList<E> GetAll()
        {
            return _session.CreateQuery("From "+typeof(E).FullName ).List<E>();
        }

        public void Evict(E entity)
        {
            _session.Evict(entity);
        }
    }
}
